#include "calculator.h"

#include <fstream>
#include <iostream>

using namespace std;

calculator::calculator() {

}

calculator::~calculator() {

}

net_fnc opcd calculator::accept_connect( ) {

	return 0;
}

net_fnc opcd calculator::processFormula(net_in cFormula & formulaIn, net_out cFormula & formulaOut) {

	formulaOut.aFormula.assign(formulaIn.aFormula);

	string filename;
	filename.assign("result.txt");
	string *coom = new string();
	coom->assign("echo '");
	coom->append(formulaIn.aFormula.c_str());
	coom->append("' | bc -l > ");
	coom->append(filename.c_str());	

	system(coom->c_str());
	
	ifstream in;					  
	string buffline;
	in.open(filename.c_str());
	getline(in, buffline);
	if ( buffline.length() != 0 ) {
		formulaOut.aResult.assign(buffline);
	} else {
		formulaOut.aResult.assign("ERROR");
	}
	in.close();
	in.clear();
	
	return 0;
}